﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algobox.JuicyLib.Apps.NewedgeDownloader
{
    public static class FileCoraxPendingDividends
    {
        const char SEPERATOR_ROW = '\n';
        const char SEPERATOR_COL = ',';
        const int MIN_IX = 15;
        const int IX_PORTFOLIO = 0;
        const int IX_SEDOL = 2;
        const int IX_ISIN = 3;
        const int IX_RIC = 4;
        const int IX_BBG = 5;
        const int IX_NAME = 6;
        const int IX_CCY = 7;
        const int IX_DIV = 11;
        const int IX_PAYDATE = 13;
        const int IX_EXDATE = 14;
        const int IX_RECORDDATE = 15;

        public static void ProcessData(string data, DateTime fileDate)
        {            
            DateTime paymentDate, exDate, recordDate;
            decimal dividend;
            int retval;

            // remove strings in inverted commas from data completely
            string[] dataArray = data.Split('"');
            data = String.Empty;
            for (int i = 0; i < dataArray.Length; i++)
            {
                if (i % 2 == 0)
                    data += dataArray[i];
            }

            var query = from csvline in data.Split(SEPERATOR_ROW)
                        let csv = csvline.Trim().Split(SEPERATOR_COL)
                        where csv.Length >= MIN_IX
                        select new
                        {
                            Portfolio = csv[IX_PORTFOLIO],
                            Sedol = csv[IX_SEDOL],
                            ISIN = csv[IX_ISIN],
                            RIC = csv[IX_RIC],
                            Bloomberg = csv[IX_BBG],
                            Name = csv[IX_NAME],
                            Currency = csv[IX_CCY],
                            Dividend = csv[IX_DIV],
                            PaymentDate = csv[IX_PAYDATE],
                            ExDate = csv[IX_EXDATE],
                            RecordDate = csv[IX_RECORDDATE]
                        };

            var enumerableQuery = query.GetEnumerator();
            for (int i = 0; enumerableQuery.MoveNext(); i++)
            {
                if (i == 0)
                    continue;

                var row = enumerableQuery.Current;

                try
                {

                    if (!Decimal.TryParse(row.Dividend, out dividend))
                    {
                        JuicyLib.SQL.LiquidView.LiquidViewHelper.LogException("NewedgeDownloader", "CoraxPendingDivs - Failed to parse dividend for record RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate);
                        Console.WriteLine("CoraxPendingDivs Can't parse price:  " + row.Dividend);
                        continue;
                    }
                    if (!DateTime.TryParseExact(row.PaymentDate,
                            "dd-MM-yyyy",
                            System.Globalization.CultureInfo.InvariantCulture,
                            System.Globalization.DateTimeStyles.None,
                            out paymentDate))
                    {
                        if (!DateTime.TryParseExact(row.PaymentDate,
                            "dd/MM/yyyy",
                            System.Globalization.CultureInfo.InvariantCulture,
                            System.Globalization.DateTimeStyles.None,
                            out paymentDate))
                        {
                            JuicyLib.SQL.LiquidView.LiquidViewHelper.LogException("NewedgeDownloader", "CoraxPendingDivs - Failed to parse PaymentDate for record RIC({0}) Account({1}) Dividend({2}) PaymentDate({3})", row.RIC, row.Portfolio, row.Dividend, row.PaymentDate);
                            Console.WriteLine("CoraxPendingDivs Can't parse PaymentDate:  " + row.PaymentDate);
                            continue;
                        }
                    }
                    if (!DateTime.TryParseExact(row.ExDate,
                            "dd-MM-yyyy",
                            System.Globalization.CultureInfo.InvariantCulture,
                            System.Globalization.DateTimeStyles.None,
                            out exDate))
                    {
                        if (!DateTime.TryParseExact(row.ExDate,
                            "dd/MM/yyyy",
                            System.Globalization.CultureInfo.InvariantCulture,
                            System.Globalization.DateTimeStyles.None,
                            out exDate))
                        {
                            JuicyLib.SQL.LiquidView.LiquidViewHelper.LogException("NewedgeDownloader", "CoraxPendingDivs - Failed to parse ExDate for record RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate);
                            Console.WriteLine("CoraxPendingDivs Can't parse ExDate:  " + row.ExDate);
                            continue;
                        }
                    }
                    if (!DateTime.TryParseExact(row.RecordDate,
                            "dd-MM-yyyy",
                            System.Globalization.CultureInfo.InvariantCulture,
                            System.Globalization.DateTimeStyles.None,
                            out recordDate))
                    {
                        if (!DateTime.TryParseExact(row.RecordDate,
                            "dd/MM/yyyy",
                            System.Globalization.CultureInfo.InvariantCulture,
                            System.Globalization.DateTimeStyles.None,
                            out recordDate))
                        {
                            JuicyLib.SQL.LiquidView.LiquidViewHelper.LogException("NewedgeDownloader", "CoraxPendingDivs - Failed to parse RecordDate for record RIC({0}) Account({1}) Dividend({2}) RecordDate({3})", row.RIC, row.Portfolio, row.Dividend, row.RecordDate);
                            Console.WriteLine("CoraxPendingDivs Can't parse RecordDate:  " + row.RecordDate);
                            continue;
                        }
                    }


                }
                catch
                {
                    continue;
                }

                retval = JuicyLib.SQL.LiquidView.LiquidViewHelper.ImportDividendByISIN(row.ISIN, row.Sedol, row.Currency, exDate, dividend, row.Currency, recordDate, paymentDate);

                if (retval == 0)
                {
                    //JuicyLib.SQL.LiquidView.LiquidViewHelper.LogException("NewedgeDownloader", "CoraxPendingDivs - Successfully imported DIV - RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate);

                    Console.WriteLine("CoraxPendingDivs Successfully imported DIV - RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate);
                }
                else if (retval == 1)
                {
                    Console.WriteLine("CoraxPendingDivs Successfully updated DIV - RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate);
                }
                else if (retval == 2)
                {
                    JuicyLib.SQL.LiquidView.LiquidViewHelper.LogException("NewedgeDownloader", "CoraxPendingDivs - Warning changed dividend div price - RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate);

                    Console.WriteLine("CoraxPendingDivs Changed DIV - RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate);
                }
                else if (retval == -1)
                {
                    Console.WriteLine("CoraxPendingDivs Failed to import DIV - Returned({4}) RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate, retval);
                }
                else if (retval == -2)
                {
                    Console.WriteLine("CoraxPendingDivs Failed to import DIV - Invalid currency - Returned({4}) Currency({5}) RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate, retval, row.Currency);
                }
                else if (retval == -3)
                {
                    Console.WriteLine("CoraxPendingDivs Failed to import DIV - Unable to find instrument - Returned({4}) RIC({0}) Account({1}) Dividend({2}) ExDate({3})", row.RIC, row.Portfolio, row.Dividend, row.ExDate, retval);
                }

            }
           
        }



    }
}
